//
//  LGReportDetailInspectTargetCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/14.
//  报告详情 - 辅助检查 -- 指标

#import "LGReportDetailInspectTargetCell.h"

#define itemHeight   viewPix(34)

@interface LGReportDetailInspectTargetCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGReportDetailInspectTargetCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.dateLabel.text = LGNSString(dataDic[@"report_time"]);
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    for (UILabel *label in self.itemArray) {
        [label removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    NSArray *indicator = dataDic[@"indicator"];
    CGFloat itemTop = (indicator.count>0)?viewPix(5):0;
    CGFloat bottom = 0;
    for (NSInteger i=0; i<indicator.count; i++) {
       bottom = [self createItemWithDic:indicator[i] top:bottom];
    }
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(itemTop);
        make.height.equalTo(@(bottom));
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.dateLabel];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.itemView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(10));
        make.left.equalTo(self.contentView).offset(viewPix(32));
        make.width.equalTo(@(viewPix(100)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.left.equalTo(self.dateLabel.mas_right);
        make.top.equalTo(self.dateLabel);
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(5));
        make.left.equalTo(self.dateLabel);
        make.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.contentView).offset(-viewPix(5));
        make.height.equalTo(@(0));
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#1F6CC6" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _dateLabel;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#1F6CC6" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:0];
    }
    return _titleLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc] init];
    }
    return _itemView;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(CGFloat)createItemWithDic:(NSDictionary *)dataDic top:(CGFloat)top{
    CGFloat bottom = top;
    NSString *name = LGNSString(dataDic[@"name"]);
    NSString *result = LGNSString(dataDic[@"result"]);
    CGFloat nameHeight = [name textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(170)];
    CGFloat resultHeight = [result textHeightWithFont:LGFont(15) maxWidth:viewPix(114)];
    CGFloat maxHeight = (nameHeight>resultHeight)?nameHeight:resultHeight;
    UILabel *titleLabel = [UILabel lableWithText:LGNSString(dataDic[@"name"]) colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
    titleLabel.frame = CGRectMake(0, top+viewPix(8), Screen_W-viewPix(170), nameHeight+2);
    [self.itemView addSubview:titleLabel];
    
    UILabel *resultLabel = [UILabel lableWithText:LGNSString(dataDic[@"result"]) colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentRight lines:0];
    [self.itemView addSubview:resultLabel];
    [resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(viewPix(5));
        make.right.equalTo(self.itemView);
        make.top.equalTo(titleLabel);
    }];
    [self.itemArray addObject:titleLabel];
    [self.itemArray addObject:resultLabel];
    bottom = top+maxHeight+viewPix(16);
    return bottom;
}

@end
